
module Cliente where

import IO
import System

import Arquivo

type File = String
-- Id, Nome, RG, Fone
type Client = (Integer, String, String, String)
type Clients = [Client]

readClientes :: File -> IO Clients
readClientes fname = do contents <- readFile fname
                        return (read contents)
                    
--clientes :: Client    
--clientes = [(1,"Marcio Alves", "123456", "32323232"), 
--            (2,"Felipe Moura", "654321", "55555555")]
            
saveClientes :: File -> Clients -> IO()
saveClientes fname [] = return ()
saveClientes fname ((a,b,c,d):x) = do print b
                                      saveClientes fname x
                                      
writeClientes :: Clients -> File ->  IO()
writeClientes clilista fname = do pt_arq <- abreArquivo fname WriteMode
                                  print "aq"
                                  hPutStr pt_arq (converteCli clilista)
                                  hClose pt_arq
                                  print "Cliente inserido com sucesso"

converteCli :: Clients -> String
converteCli clilista = ("["++converteCliAux clilista)

converteCliAux :: Clients -> String
converteCliAux [] = "]"
converteCliAux ((a,b,c,d):clilista) = "("++ show a++", "++b++", "++c++", "++d++"),"   
converteCliAux ((a,b,c,d):[]) = "("++show a++", "++b++", "++c++", "++d++")]"

addClientes :: Client -> File -> IO()
addClientes cli fname = do clilist <- readClientes fname
                           let newclilist = (cli:clilist)
                           writeClientes newclilist fname


novocli = (3,"cris","315615","2222222")

rc :: IO()
rc = do cl <- readClientes "cliente.txt"
        saveClientes "iu" cl
        
ac = addClientes novocli "cliente.txt"